package com.example.edu_service.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.edu_service.client.VodClient;
import com.example.edu_service.entity.EduVideo;
import com.example.edu_service.mapper.EduVideoMapper;
import com.example.edu_service.service.EduVideoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程视频 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2021-08-02
 */
@Service
public class EduVideoServiceImpl extends ServiceImpl<EduVideoMapper, EduVideo> implements EduVideoService {


    @Autowired
    private VodClient vodClient;
    @Override
    public void removeVideoByCourseId(String courseId) {

        //根据courseId查询videoId
        QueryWrapper<EduVideo> wrapperVideo=new QueryWrapper<>();
        wrapperVideo.eq("course_id",courseId);
        wrapperVideo.select("video_source_id");
        List<EduVideo> videoList = baseMapper.selectList(wrapperVideo);
        //将得到的videoId封装到list集合
        List<String> videoIds = new ArrayList<>();
        for (int i = 0; i <videoList.size() ; i++) {
            EduVideo eduVideo=videoList.get(i);
            String videoSourceId = eduVideo.getVideoSourceId();

            if (StringUtils.hasLength(videoSourceId)) {
                videoIds.add(videoSourceId);
            }
        }
        //删除多个video
        if (videoIds.size()>0){
            vodClient.deleteVideoes(videoIds);
        }
//        vodClient.deleteVideoList(videoIds);
        //删除小节
        QueryWrapper<EduVideo> wrapperCourse=new QueryWrapper<>();
        wrapperCourse.eq("course_id",courseId);
        baseMapper.delete(wrapperCourse);
    }
}
